﻿using LibraryProject.Model;
using LibraryProject.Tool;
using MongoDB.Bson;
using MongoDB.Driver;

namespace LibraryProject.Service.Implement;

public class MongodbService : IMongodbService {
    // 腾讯云服务器的mongodb服务
    private const string ConnectionString
        = "mongodb://root:123456@111.229.78.126:27017";

    // 数据库名称
    private const string DbName = "linkFileManage";

    // 表名
    private const string LinkFileCollectionName = "linkFile";

    // 获取数据库链接
    private MongoClient? _connection;

    // 初始化数据库链接
    private MongoClient Connection =>
        _connection ??= new MongoClient(ConnectionString);

    // 通过AccountId查询
    public async Task<MongodbAccount?> QueryByAccountId(string accountId) {
        var collection = Connection.GetDatabase(DbName).GetCollection<MongodbAccount>(LinkFileCollectionName);
        var filter = Builders<MongodbAccount>.Filter.Eq("accountId", accountId);
        var result = await collection.Find(filter).FirstOrDefaultAsync();
        return result;
    }

    // 添加账号
    public async Task AddAccount(string accountId) {
        // 插入初始化数据
        var data = System.Text.Json.JsonSerializer.Serialize(LinkFileTool.SampleLinkFiles());
        var mongodbAccount = new MongodbAccount() { AccountId = accountId, Data = data };

        await Connection.GetDatabase(DbName)
            .GetCollection<BsonDocument>(LinkFileCollectionName)
            .InsertOneAsync(mongodbAccount.ToBsonDocument());
    }

    // mongodb数据库的count自增
    public async Task IncrementDeviceCount(string accountId, int count) {
        var collection = Connection.GetDatabase(DbName).GetCollection<MongodbAccount>(LinkFileCollectionName);
        var filter = Builders<MongodbAccount>.Filter.Eq("accountId", accountId);
        var update = Builders<MongodbAccount>.Update.Inc("count", count + 1);

        await collection.UpdateOneAsync(filter, update);
    }

    // 修改mongodb数据库的operator
    public async Task UpdateOperatorByAccountId(string accountId, int deviceId) {
        var collection = Connection.GetDatabase(DbName).GetCollection<MongodbAccount>(LinkFileCollectionName);
        var filter = Builders<MongodbAccount>.Filter.Eq("accountId", accountId);
        var update = Builders<MongodbAccount>.Update.Set("operator", deviceId.ToString());
        await collection.UpdateOneAsync(filter, update);
    }

    // 修改mongodb数据库的data
    public async Task UpdateDataByAccountId(string accountId, string data) {
        var collection = Connection.GetDatabase(DbName).GetCollection<MongodbAccount>(LinkFileCollectionName);
        var filter = Builders<MongodbAccount>.Filter.Eq("accountId", accountId);
        var update = Builders<MongodbAccount>.Update.Set("data", data);
        await collection.UpdateOneAsync(filter, update);
    }

    // 根据accountId修改mongodb数据库的指定属性
    public async Task UpdateAttrByAccountId(string accountId, string target, string data) {
        var collection = Connection.GetDatabase(DbName).GetCollection<MongodbAccount>(LinkFileCollectionName);
        var filter = Builders<MongodbAccount>.Filter.Eq("accountId", accountId);
        var update = Builders<MongodbAccount>.Update.Set(target, data);
        await collection.UpdateOneAsync(filter, update);
    }
}